﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class CellString
{
    public Player m_BelongTo;
    public List<Vector2Int> m_CellPositions;
    int m_MinX;
    int m_MaxX;
    int m_MinY;
    int m_MaxY;
    public CellString()
    {
        
    }
    public static CellString Create()
    {
        CellString ret = ObjectPool<CellString>.Claim();
        ret.Init();
        return ret;
    }
    void Init()
    {
        m_CellPositions = ListPool<Vector2Int>.Claim();
        m_BelongTo = Player.None;
        m_MinX = m_MinY = 999;
        m_MaxX = m_MaxY = -1;
    }
    public static CellString CopyFrom(CellString other)
    {
        CellString cellString = CellString.Create();
        cellString.m_BelongTo = other.m_BelongTo;
        cellString.m_CellPositions = ListPool<Vector2Int>.Claim();
        cellString.m_CellPositions.AddRange(other.m_CellPositions);
        cellString.m_MinX = other.m_MinX;
        cellString.m_MaxX = other.m_MaxX;
        cellString.m_MinY = other.m_MinY;
        cellString.m_MaxY = other.m_MaxY;
        return cellString;
    }
    public void Release()
    {
        ListPool<Vector2Int>.Release(m_CellPositions);
        ObjectPool<CellString>.Release(this);
    }
    public void AddCell(Vector2Int pos)
    {
        m_CellPositions.Add(pos);
        m_MinX = Mathf.Min(m_MinX, pos.x);
        m_MaxX = Mathf.Max(m_MaxX, pos.x);
        m_MinY = Mathf.Min(m_MinY, pos.y);
        m_MaxY = Mathf.Max(m_MaxY, pos.y);
    }
    public bool IsComplete
    {
        get
        {
            if (m_BelongTo == Player.Red)
            {
                return m_MinX == 0 && m_MaxX == GameConfig.Width - 1;
            }
            else if (m_BelongTo == Player.Blue)
            {
                return m_MinY == 0 && m_MaxY == GameConfig.Height - 1;
            }
            return false;
        }
    }
}